//
//  WelcomeViewController.swift
//  WeiBoOfSwift
//
//  Created by aven wu on 25/04/2018.
//  Copyright © 2018 GML. All rights reserved.
//

import UIKit

class WelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 1.添加子控件
        view.addSubview(bgIV)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        bgIV.snp.makeConstraints { (make) -> Void in
            make.centerX.equalToSuperview()
            make.height.equalToSuperview()
            make.width.equalToSuperview()
        }
        iconView.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(self.view.snp.top).offset(SAFE_SCREEN_TOP+100)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(170/2)
        }
        messageLabel.snp.makeConstraints { (make) ->Void in
            make.bottom.equalTo(self.iconView.snp.bottom).offset(50)
            make.centerX.equalToSuperview()
        }
        
        // 3.设置用户头像
        if let iconUrl = UserOauthModel.readAccount()?.avatar_large
        {
            let url = NSURL(string: iconUrl)!
            iconView.setImageWith(url as URL)
        }
        
    }
    override func viewDidAppear(_ animated: Bool) {
        let startY = (UIScreen.main.bounds.height - SAFE_SCREEN_TOP)
        print(startY)

        iconView.transform = CGAffineTransform.init(translationX: 0, y: startY)
        // 3.执行动画
        UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 头像动画
            self.iconView.transform = CGAffineTransform.identity
        }) { (_) -> Void in
            
            // 文本动画
            UIView.animate( withDuration: 2.0, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                self.messageLabel.alpha = 1.0
                
            }, completion: { (_) -> Void in
                print("OK")
                // 切换控制器，展示首页
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: SwitchRootViewControllerKey), object: true)
                
            })
        }
        
    }

    
    // MARK: -懒加载
    private lazy var bgIV: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
//        let width = UIImage(named: "avatar_default_big")?.size.width
        iv.layer.cornerRadius = 170 / 4
        iv.clipsToBounds = true
        return iv
    }()
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.sizeToFit()
        label.alpha = 0.0
        return label
    }()

}
